﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using eeenno.ReaderEngine;

namespace eeenno.ReaderEngine.Objects
{
    [Serializable]

    /// <summary>
    /// Collection of Google Reader subscription objects
    /// </summary>
    public class Subscriptions
    {        
        private List<Subscription> _items;

        public Subscriptions()
        {
            _items = new List<Subscription>(10);
        }

        public Subscription Get(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                if (name == _items[i].title)
                    return _items[i];
            }
            return null;
        }

        public Subscription Get(int index)
        {           
            return _items[index];
        }

        public void Set(int index, Subscription value)
        {
            if (value != null)
            {
                _items[index] = value;
            }
        }

        public void Add(Subscription item)
        {
            if (item != null)
            {
                _items.Add(item);
            }
        }

        public void Remove(Subscription item)
        {
            if (item != null)
            {
                _items.Remove(item);
            }
        }

        public int Count
        {
            get { return _items.Count; }
        }

        public void MarkAllSubscriptionsRead()
        {
            for (int i = 0; i < Count; i++)
            {
                if (Get(i).articles_loaded)
                {
                    Get(i).articles.MarkAllArticlesRead();
                }
            }
        }

        public int unread_count
        {
            get { return GetUnreadCount(); }
        }

        public int article_count
        {
            get { return GetArticleCount(); }
        }

        private int GetArticleCount()
        {
            int ret_val = 0;
            for (int i = 0; i < Count; i++)
            {
                ret_val += Get(i).article_count;
            }
            return ret_val;
        }

        private int GetUnreadCount()
        {
            int ret_val = 0; 
            for (int i = 0; i < Count; i++)
            {
                //so we don't count the unreads in each tags reading list
                if (!Get(i).is_reading_list)
                {
                    ret_val += Get(i).unread_count;
                }
            }
            return ret_val;
        }

        /// <summary>
        /// Searches through every subscription for the article with the ID, will return null if it doesn't exist
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Article GetArticleByID(string ID)
        {
            for (int i = 0; i < Count; i++)
            {
                //if the articles aren't loaded yet
                if (Get(i).article_count > 0)
                {
                    if (Get(i).articles.Get(ID) != null && !Get(i).is_reading_list)
                    {
                        return Get(i).articles.Get(ID);
                    }
                }
            }

            return null;
        }
    }
}
